//
//  ReadScrollListViewController.m
//  product_B
//
//  Created by lanou on 16/5/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadScrollListViewController.h"
#import "ReadScrollListTableViewCell.h"
#import "ReadScrollListViewModel.h"
#import "ReadTableViewController.h"
@interface ReadScrollListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *modelArray;
@property (nonatomic,strong)UIImageView *imageView;
@end

@implementation ReadScrollListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.tableView.backgroundColor = Kcolor(arc4random() % 256, arc4random() % 256, arc4random() % 256);
    self.navigationItem.title = @"返回";
    [_tableView registerNib:[UINib nibWithNibName:@"ReadScrollListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 2;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self requestWork];
    self.tableView.tableHeaderView = [self headView];
    self.tableView.tableFooterView = [self footView];
}
- (void)requestWork{
    [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/reading/carousel/%@",_model.modelId] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.modelArray = [ReadScrollListViewModel readScrollListViewModelWithConfigure:dic];
        [_tableView reloadData];
    } errorBlock:^(NSError *error) {
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadScrollListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ReadScrollListViewModel *model = self.modelArray[indexPath.row];
    cell.titleLabel.text = [NSString stringWithFormat:@"%ld %@",indexPath.row+1,model.title];
    cell.authorLabel.text = model.author;
    cell.contentLabel.text = model.introduction;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (IBAction)backButtonAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (UIView *)headView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 100)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(125, 40, 125, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = _model.title;
    [view addSubview:label];
    return view;
}
- (UIView *)footView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kHeight - kWidth * 329 / 800, kWidth, kWidth * 329 / 800)];
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_model.cover]];
    [view addSubview:_imageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, (kHeight - kWidth * 329 / 800 - 60)/2, kWidth - 40, 80)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = _model.bottom_text;
    label.numberOfLines = 0;
    [view addSubview:label];
    return view;
}
//只要滚动了就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    
    CGFloat offsetY = scrollView.contentOffset.y - scrollView.contentSize.height+kHeight;
    if (offsetY > 0) {
//        _imageView.transform = CGAffineTransformMakeScale(1 + offsetY / _imageView.frame.size.width, 1+ offsetY / _imageView.frame.size.height);
        _imageView.frame = CGRectMake(0 - offsetY / 2, kHeight - kWidth * 329 / 800, kWidth + offsetY, kWidth * 329 / 800 + offsetY);
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadTableViewController *tableVC = [[ReadTableViewController alloc]init];
    ReadScrollListViewModel *model = self.modelArray[indexPath.row];
    tableVC.modelID = model.item_id;
    tableVC.indexY = model.type.intValue - 1;
    tableVC.isAdd = YES;
//    [self presentViewController:tableVC animated:YES completion:nil];
    [self.navigationController pushViewController:tableVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
